package com.xintonghua.bussiness.ui.fragment.client.user;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.jcodecraeer.xrecyclerview.XRecyclerView;
import com.xintonghua.base.utils.RefreshUtils;
import com.xintonghua.bussiness.R;
import com.xintonghua.bussiness.adapter.ConsumeAdapter;
import com.xintonghua.bussiness.adapter.OrderHomeAdapter;
import com.xintonghua.bussiness.api.myinterface.DataCallBack;
import com.xintonghua.bussiness.base.BaseActivity;
import com.xintonghua.bussiness.bean.ConsumeRespone;
import com.xintonghua.bussiness.bean.Contant;
import com.xintonghua.bussiness.bean.GoodsManageResponse;
import com.xintonghua.bussiness.bean.ShopOrderListBean;
import com.xintonghua.bussiness.bean.UserInfoBean;
import com.xintonghua.bussiness.ui.fragment.client.SelectsSexActivity;
import com.xintonghua.bussiness.util.CalendarTools;
import com.xintonghua.bussiness.util.JsonUtil;
import com.xintonghua.bussiness.util.JumpUtils;
import com.xintonghua.bussiness.util.MyState;
import com.xintonghua.bussiness.util.TimeCallback;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.addapp.pickers.picker.DatePicker;

/**
 * 作者：wanxianyi
 * 时间：2018/3/5.
 * 包名：com.xintonghua.bussiness.ui.fragment.client.user
 * <p>
 * 消费记录
 * 完善liqi
 */

public class ConsumeActivity extends BaseActivity implements XRecyclerView.LoadingListener {
    @BindView(R.id.tv_last_three)
    TextView tvLastThree;
    @BindView(R.id.ll_last_three)
    LinearLayout llLastThree;
    @BindView(R.id.tv_time)
    TextView tvTime;
    @BindView(R.id.ll_time)
    LinearLayout llTime;
    @BindView(R.id.lv_consume)
    XRecyclerView lvConsume;
    private int page = 1;
    private int pageSize = 10;
    Contant contantbean;
    ConsumeAdapter adapter;
    UserInfoBean userInfoBean;
    List<ShopOrderListBean.ListBean> list = new ArrayList<ShopOrderListBean.ListBean>();

    //          bun2.putSerializable("data0", (Serializable) contantbean);
//                bun2.putSerializable("data1", (Serializable) userInfoBean);
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_consume);
        ButterKnife.bind(this);
        contantbean = (Contant) JumpUtils.getSerializable(this, "data0");
        initUI();
    }

    @Override
    public void dataBack(Object data, int order) {
        super.dataBack(data, order);

        try {
            switch (order) {
                case MyState.requestOne:
                    ShopOrderListBean bean = JsonUtil.getEntityByJsonString(data.toString(), ShopOrderListBean.class);
                    if (page == 1) {
                        list.clear();
                    }
                    adapter.addAll(bean.getList());
                    adapter.notifyDataSetChanged();
                    lvConsume.refreshComplete();
                    lvConsume.loadMoreComplete();
                    break;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void initUI() {
        super.initUI();
        onSimpleActionBar();
        setSimpleActionBar("消费记录");
        RefreshUtils.initList(this, lvConsume);
        adapter = new ConsumeAdapter(this, list);
        lvConsume.setAdapter(adapter);
        getNetData();
    }


    @Override
    public void onRefresh() {
        page = 1;
        getNetData();
    }

    @Override
    public void onLoadMore() {
        getNetData();
        page++;
    }

    @OnClick({R.id.ll_last_three, R.id.ll_time})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.ll_last_three:
                JumpUtils.jumpActivityForResult(ConsumeActivity.this, SelectTianShuActivity.class, null, MyState.requestTwo);
                break;
            case R.id.ll_time:
                CalendarTools.getInstance().createDateDialog(ConsumeActivity.this, tvTime, new TimeCallback() {
                    @Override
                    public void timeCallback(String year, String month, String day) {
                        page = 1;
                        times = year.substring(year.length() - 2, year.length());
                        pageSize = 10;
                        onRefresh();
                    }
                });
                break;
        }
    }


    String times = "";

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == MyState.requestTwo && resultCode == Activity.RESULT_OK) {

            if (data != null) {
                String tianshu = data.getStringExtra("data");
                list.clear();
                adapter.notifyDataSetChanged();
                chongzhiTab();
                showTianShu(tianshu);
                onRefresh();
            }
        }
    }

    public void onYearMonthDayPicker(View view) {
        Calendar calendar = Calendar.getInstance();
        int year = calendar.get(Calendar.YEAR);
        int month = calendar.get(Calendar.MONTH) + 1;
        int day = calendar.get(Calendar.DAY_OF_MONTH);
        final DatePicker picker = new DatePicker(ConsumeActivity.this);
        picker.setCanLoop(true);
        picker.setWheelModeEnable(true);
        picker.setTopPadding(15);
        picker.setRangeStart(1790, 8, 29);
        picker.setRangeEnd(2111, 1, 11);
        picker.setSelectedItem(year, month, day);
        picker.setWeightEnable(true);
        picker.setOnDatePickListener(new DatePicker.OnYearMonthDayPickListener() {
            @Override
            public void onDatePicked(String year, String month, String day) {

            }
        });
        picker.setOnWheelListener(new DatePicker.OnWheelListener() {

            @Override
            public void onYearWheeled(int index, String year) {
                picker.setTitleText(year + "-" + picker.getSelectedMonth() + "-" + picker.getSelectedDay());
            }

            @Override
            public void onMonthWheeled(int index, String month) {
                picker.setTitleText(picker.getSelectedYear() + "-" + month + "-" + picker.getSelectedDay());
            }

            @Override
            public void onDayWheeled(int index, String day) {
                picker.setTitleText(picker.getSelectedYear() + "-" + picker.getSelectedMonth() + "-" + day);
            }
        });
        picker.show();
    }

    public void chongzhiTab() {
        pageSize = 10;
    }

    public void showTianShu(String tianshu) {
        switch (tianshu) {
            case "3":
                pageSize = 3;
                tvLastThree.setText("最近三天");
                break;
            case "7":
                pageSize = 7;
                tvLastThree.setText("最近一周");
                break;
            case "30":
                pageSize = 30;
                tvLastThree.setText("最近一个月");
                break;
            default:
                tvLastThree.setText("请选择时间");
                tvLastThree.setTextColor(Color.parseColor("sd"));
                break;
        }
    }

    public void getNetData() {
        httpCent.getRecordConsumeList("" + contantbean.getId(), "" + page, "" + pageSize, "" + times, this, MyState.requestOne);
    }
}

interface initTab {

}